/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.Project;
import com.jeeplus.modules.infor.mapper.ProjectMapper;
import com.jeeplus.modules.infor.entity.MileageScale;
import com.jeeplus.modules.infor.mapper.MileageScaleMapper;
import com.jeeplus.modules.infor.entity.SettlementMode;
import com.jeeplus.modules.infor.mapper.SettlementModeMapper;
import com.jeeplus.modules.infor.entity.SettlementType;
import com.jeeplus.modules.infor.mapper.SettlementTypeMapper;
import com.jeeplus.modules.infor.entity.Template;
import com.jeeplus.modules.infor.mapper.TemplateMapper;
import com.jeeplus.modules.infor.entity.TrafficScale;
import com.jeeplus.modules.infor.mapper.TrafficScaleMapper;

/**
 * 项目Service
 * @author 李超辉
 * @version 2017-11-22
 */
@Service
@Transactional(readOnly = true)
public class ProjectService extends CrudService<ProjectMapper, Project> {

	@Autowired
	private MileageScaleMapper mileageScaleMapper;
	@Autowired
	private SettlementModeMapper settlementModeMapper;
	@Autowired
	private SettlementTypeMapper settlementTypeMapper;
	@Autowired
	private TemplateMapper templateMapper;
	@Autowired
	private TrafficScaleMapper trafficScaleMapper;
	
	public Project get(String id) {
		Project project = super.get(id);
		project.setMileageScaleList(mileageScaleMapper.findList(new MileageScale(project)));
		project.setSettlementModeList(settlementModeMapper.findList(new SettlementMode(project)));
		project.setSettlementTypeList(settlementTypeMapper.findList(new SettlementType(project)));
		project.setTemplateList(templateMapper.findList(new Template(project)));
		project.setTrafficScaleList(trafficScaleMapper.findList(new TrafficScale(project)));
		return project;
	}
	
	public List<Project> findList(Project project) {
		return super.findList(project);
	}
	
	public Page<Project> findPage(Page<Project> page, Project project) {
		return super.findPage(page, project);
	}
	
	@Transactional(readOnly = false)
	public void save(Project project) {
		super.save(project);
		for (MileageScale mileageScale : project.getMileageScaleList()){
			if (mileageScale.getId() == null){
				continue;
			}
			if (MileageScale.DEL_FLAG_NORMAL.equals(mileageScale.getDelFlag())){
				if (StringUtils.isBlank(mileageScale.getId())){
					mileageScale.setProject(project);
					mileageScale.preInsert();
					mileageScaleMapper.insert(mileageScale);
				}else{
					mileageScale.preUpdate();
					mileageScaleMapper.update(mileageScale);
				}
			}else{
				mileageScaleMapper.delete(mileageScale);
			}
		}
		for (SettlementMode settlementMode : project.getSettlementModeList()){
			if (settlementMode.getId() == null){
				continue;
			}
			if (SettlementMode.DEL_FLAG_NORMAL.equals(settlementMode.getDelFlag())){
				if (StringUtils.isBlank(settlementMode.getId())){
					settlementMode.setProject(project);
					settlementMode.preInsert();
					settlementModeMapper.insert(settlementMode);
				}else{
					settlementMode.preUpdate();
					settlementModeMapper.update(settlementMode);
				}
			}else{
				settlementModeMapper.delete(settlementMode);
			}
		}
		for (SettlementType settlementType : project.getSettlementTypeList()){
			if (settlementType.getId() == null){
				continue;
			}
			if (SettlementType.DEL_FLAG_NORMAL.equals(settlementType.getDelFlag())){
				if (StringUtils.isBlank(settlementType.getId())){
					settlementType.setProject(project);
					settlementType.preInsert();
					settlementTypeMapper.insert(settlementType);
				}else{
					settlementType.preUpdate();
					settlementTypeMapper.update(settlementType);
				}
			}else{
				settlementTypeMapper.delete(settlementType);
			}
		}
		for (Template template : project.getTemplateList()){
			if (template.getId() == null){
				continue;
			}
			if (Template.DEL_FLAG_NORMAL.equals(template.getDelFlag())){
				if (StringUtils.isBlank(template.getId())){
					template.setProject(project);
					template.preInsert();
					templateMapper.insert(template);
				}else{
					template.preUpdate();
					templateMapper.update(template);
				}
			}else{
				templateMapper.delete(template);
			}
		}
		for (TrafficScale trafficScale : project.getTrafficScaleList()){
			if (trafficScale.getId() == null){
				continue;
			}
			if (TrafficScale.DEL_FLAG_NORMAL.equals(trafficScale.getDelFlag())){
				if (StringUtils.isBlank(trafficScale.getId())){
					trafficScale.setProject(project);
					trafficScale.preInsert();
					trafficScaleMapper.insert(trafficScale);
				}else{
					trafficScale.preUpdate();
					trafficScaleMapper.update(trafficScale);
				}
			}else{
				trafficScaleMapper.delete(trafficScale);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Project project) {
		super.delete(project);
		mileageScaleMapper.delete(new MileageScale(project));
		settlementModeMapper.delete(new SettlementMode(project));
		settlementTypeMapper.delete(new SettlementType(project));
		templateMapper.delete(new Template(project));
		trafficScaleMapper.delete(new TrafficScale(project));
	}
	
}